package com.suneast.suneaststore.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.suneast.suneaststore.R;

public class CancelConfirmDialog extends DialogFragment implements View.OnClickListener {
    private TextView tvContent;
    private TextView tvTitle;
    private Button btns[] = new Button[2];
    private String title = "提示";
    private String content;
    private View splitterBtn;

    private int style = 0;

    public final static int STYLE_DEFAULT = 0;
    public final static int HIDE_BTN_LEFT = 2;
    public final static int HIDE_BTN_RIGHT = 1;
    public final static int HIDE_TITLE = 4;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
            lp.width = 300;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
        View view = inflater.inflate(R.layout.dialog_delete_address_tip, container, false);
        tvContent = view.findViewById(R.id.tv_content);
        tvTitle = view.findViewById(R.id.tv_title);
        splitterBtn = view.findViewById(R.id.splitter_btn);

        if (savedInstanceState != null) {
            title = savedInstanceState.getString("title", "提示");
            content = savedInstanceState.getString("content", "");
            style = savedInstanceState.getInt("style", 0);
        } else if (getArguments() != null) { // 此处兼容zest以前写的东西,新用法可以动态设置内容,不一样的提示可以复用一个dialogfragment
            content = getArguments().getString("content", ""); // 默认最好不要显示任何字
        }
        tvTitle.setText(title);
        tvContent.setText(content);
        btns[0] = view.findViewById(R.id.btn_left);
        btns[0].setOnClickListener(this);
        btns[1] = view.findViewById(R.id.btn_right);
        btns[1].setOnClickListener(this);
        tvTitle.setVisibility((style & HIDE_TITLE) > 0 ? View.GONE : View.VISIBLE);
        splitterBtn.setVisibility((style & (HIDE_BTN_LEFT | HIDE_BTN_RIGHT)) > 0 ? View.GONE : View.VISIBLE);
        btns[0].setVisibility((style & HIDE_BTN_LEFT) > 0 ? View.GONE : View.VISIBLE);
        btns[1].setVisibility((style & HIDE_BTN_RIGHT) > 0 ? View.GONE : View.VISIBLE);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("content", content);
        outState.putString("title", title);
        outState.putInt("style", style);
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            if (v.getId() == R.id.btn_left) {
                listener.onCancel();
            } else if (v.getId() == R.id.btn_right) {
                listener.onConfirm();
            }
        }
    }

    public interface ConfirmBtnClickListener {
        void onConfirm();

        void onCancel();
    }

    private ConfirmBtnClickListener listener;

    public void setListener(ConfirmBtnClickListener listener) {
        this.listener = listener;
    }

    public void setTitle(String str) {
        title = str;
        if (tvTitle != null) {
            tvTitle.setText(str);
        }
    }

    public void setContent(String str) {
        content = str;
        if (tvContent != null) {
            tvContent.setText(str);
        }
    }

    public void setStyle(int flag) {
        style = flag;
        if (tvTitle != null) {
            tvTitle.setVisibility((style & HIDE_TITLE) > 0 ? View.GONE : View.VISIBLE);
            btns[0].setVisibility((style & HIDE_BTN_LEFT) > 0 ? View.GONE : View.VISIBLE);
            btns[1].setVisibility((style & HIDE_BTN_RIGHT) > 0 ? View.GONE : View.VISIBLE);
        }
    }
}
